<?php

namespace Home\Model;
use Think\Model;

class ReportOrdersModel extends Model {
	private $report_order_table = "dmp_report_orders";
	private $report_table = "dmp_report";

	private $status = true;
	private $msg = null;

	//获取用户购买报告列表数据
	public function getBuyReportList($uid=null,$page=1,$pagesize = 20){
		$where['uid'] = $uid;
		$offset = ($page-1)*$pagesize;

		$count = $this->where($where)->count();

		//todo，需要获取报告信息--多表查询
		$sql = "select orders.*,report.id rep_id,report.title,report.cover_img,report.desc,report.attachment_name ";
		$sql .=" from ".$this->report_order_table." orders left join ".$this->report_table." report on orders.report_id=report.id where orders.uid=".$uid." and orders.pay_status=1 limit ".$offset.",".$pagesize;
		$list = $this->query($sql);
		// $list = $this->where($where)->limit($offset.",".$pagesize)->select();

		$totalpage = ceil($count/$pagesize);
		return array("page"=>$page,"totalpage"=>$totalpage,"data"=>$list);
	}

	//普通用户订单
	public function getListWithUser($uid=null,$pay_status=null,$page=1,$pagesize=20){
		if(empty($uid)) return null;
		if(!is_numeric($page) || $page <1) $page = 1;
		if(!is_numeric($pagesize) || $pagesize <1) $pagesize = 1;

		$where['enable'] = 1;
		$where['uid'] = $uid;
		if(!empty($pay_status)){
			if(is_array($pay_status)){
				$where['pay_status'] = array('in',$pay_status);
			}else{
				$where['pay_status'] = $pay_status;
			}
		}

		$sql = "select orders.*,ifnull(user.user_name,0) works_name,ifnull(enter.enterprise_name,0) enter_name,user.auth_type from dmp_report_orders orders left join dmp_users user on user.id=orders.works_uid left join dmp_enterprises enter on user.enterprise_id=enter.id ";

		$sql .=" where orders.enable=1 and orders.uid=".$uid;
		if(isset($where['pay_status'])){
			if(is_array($where['pay_status'])){
				$sql .=" and orders.pay_status in (".implode(",",$where['pay_status'][1]).") ";
			}else{
				$sql .=" and orders.pay_status=".$where['pay_status']." ";
			}
		}
		
		$all_list = $this->query($sql);
		$total = count($all_list);

		$sql .=" order by orders.id desc ";
		$offset = ($page-1)*$pagesize;
		$sql .=" limit ".$offset.",".$pagesize;
		$list = $this->query($sql);

		$return['total_page'] = ceil($total/$pagesize);
		$return['list'] = $list;
		$return['current_page'] = $page;
		return $return;
	}

	// 获取所有订单，已付款订单，已取消订单
	public function getListWithWorks($works_uid,$pay_status,$enable = 1,$order_no,$start_date,$end_date,$page=1,$pagesize=20){
		if(empty($works_uid)) return null;
		if(!is_numeric($page) || $page <1) $page = 1;
		if(!is_numeric($pagesize) || $pagesize <1) $pagesize = 1;

		$where['enable'] = $enable;
		$where['works_uid'] = $works_uid;
		if(!empty($pay_status)){
			if(is_array($pay_status)){
				$where['pay_status'] = array('in',$pay_status);
			}else{
				$where['pay_status'] = $pay_status;
			}
		}
		if(!empty($start_date) && !empty($end_date)){
			$where['order_time'] = array('between',array($start_date,$end_date) );
		}else {
			if(!empty($start_date)){
				$where['order_time'] = array('gt',$start_date);
			}else if(!empty($end_date)){
				$where['order_time'] = array('lt',$end_date);
			}
		}
		if(!empty($order_no)){
			$where['order_id'] = $order_no;
		}
		
		$total = $this->where($where)->count();
		$offset = ($page-1)*$pagesize;
		$list = $this->where($where)->limit($offset.",".$pagesize)->order('id desc')->select();
		$return['total_page'] = ceil($total/$pagesize);
		$return['list'] = $list;
		$return['current_page'] = $page;
		return $return;
	}

	// 获取订单详情
	public function getInfo($order_id){
		if(empty($order_id)) return false;
		$where['order_id'] = $order_id;
		return $this->where($where)->find();
	}

	//查询用户是否已购买某报表
	public function checkExistsBuy($uid=null,$report_id=null){
		if(empty($uid) || empty($report_id)) return false;

		$where = array('uid'=>$uid,'report_id'=>$report_id,'pay_status'=>1);
		return $this->where($where)->find();
	}

	// 插入订单数据
	public function writeOrder($info){
		$data = $this->checkOrder($info);

		return $result = $this->data($data)->add();
	}

	// 数据检查
	private function checkOrder($data){
		//todo

		return $data;
	}

	// 获取插入状态
	public function getInsertStatus(){
		return $this->status;
	}

	// 获取消息
	public function getMsg(){
		return $this->msg;
	}

	//查询用户订单是否付款成功
	public function wxOrderResult($order_id){
		if(empty($order_id)) return false;

		$where['order_id'] = $order_id;
		$where['pay_status'] = 1;

		$result = $this->where($where)->count();
		return $result;
	}

	//查询订单及下单用户信息
	public function orderInfoWithBuyer($order_id = null){
		if(empty($order_id)) return null;

		$sql = "select orders.*,case auth_type when 1 then u.user_name when 2 then enter.enterprise_name end uname,u.user_email email,case auth_type when 1 then u.user_mobile when 2 then enter.contacts_mobile end user_mobile ";
		$sql .= " from dmp_report_orders orders left join dmp_users u on u.id=orders.uid left join dmp_enterprises enter on enter.id=u.enterprise_id where orders.order_id=".$order_id;

		$info = $this->query($sql);
		if(empty($info)){
			return null;
		}else{
			return $info[0];
		}
	}

	//取消订单
	public function chanelOrder($order_id=null){
		if(empty($order_id)) return false;

		$where['order_id'] = $order_id;
		$data['enable'] = 0;
		return $this->where($where)->data($data)->save();
	}
	
}

?>